package com.jzo2o.health.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.health.mapper.MemberMapper;
import com.jzo2o.health.mapper.ReservationSettingMapper;
import com.jzo2o.health.model.domain.Member;
import com.jzo2o.health.model.domain.ReservationSetting;
import com.jzo2o.health.model.dto.request.ReservationSettingUpsertReqDTO;
import com.jzo2o.health.model.dto.response.ReservationDateResDTO;
import com.jzo2o.health.model.dto.response.ReservationSettingResDTO;
import com.jzo2o.health.model.excel.ReservationImportData;
import com.jzo2o.health.service.IMemberService;
import com.jzo2o.health.service.IReservationSettingService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @Author anjing
 * @Date 2024/12/14 10:23
 */
@Service
public class IReservationSettingServiceImpl extends ServiceImpl<ReservationSettingMapper, ReservationSetting> implements IReservationSettingService {

    @Resource
    private ReservationSettingMapper reservationSettingMapper;

    @Override
    public List<ReservationSettingResDTO> getReservationByMonth(String date) {
        //在表中进行字段格式化
        return reservationSettingMapper.getReservationByMonth(date);
//        List<ReservationSetting> list = lambdaQuery().likeRight(ReservationSetting::getDate, date).groupBy(ReservationSetting::getDate).list();
//        return BeanUtil.copyToList(list,ReservationSettingResDTO.class);

    }

    @Override
    public void editNumberByDate(ReservationSettingUpsertReqDTO reservationSettingUpsertReqDTO) {

        LocalDate orderDate = reservationSettingUpsertReqDTO.getOrderDate();
        Integer number = reservationSettingUpsertReqDTO.getNumber();
        if (number>1000||number<0){
            throw new BadRequestException("可预约人数不合法");
        }
        //得到对象
        ReservationSetting one = lambdaQuery().eq(ReservationSetting::getOrderDate, orderDate).one();
        if (one!=null){
            //判断可约人数是否小于已预约人数
            if (one.getReservations()>=number){
                throw new BadRequestException("可预约人数不合法");
            }
            //改变
            lambdaUpdate().eq(ReservationSetting::getOrderDate,orderDate).set(ReservationSetting::getNumber,reservationSettingUpsertReqDTO.getNumber()).update();
        }else {
            //新增
            ReservationSetting reservationSetting = new ReservationSetting();
            reservationSetting.setOrderDate(orderDate);
            reservationSetting.setNumber(reservationSettingUpsertReqDTO.getNumber());
            save(reservationSetting);
        }
    }

    @Override
    public void upload(MultipartFile file)  {
        //
        List<ReservationImportData> list = new ArrayList<>();
        if (file.isEmpty()){
            return;
        }

        try {
            EasyExcel.read(file.getInputStream(), ReservationImportData.class, new AnalysisEventListener<ReservationImportData>() {
                @Override
                public void invoke(ReservationImportData data, AnalysisContext analysisContext) {
                    list.add(data);
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).sheet().doRead();

        }catch (IOException e){

            log.error(e.getMessage());
            throw new BadRequestException(e);
        }

        //解析list
        for (ReservationImportData data : list) {
            ReservationSetting one = this.lambdaQuery().eq(ReservationSetting::getOrderDate, data.getDate()).one();
            if (one==null){
                ReservationSetting reservationSetting = new ReservationSetting();
                reservationSetting.setNumber(data.getNumber());
                reservationSetting.setOrderDate(LocalDate.parse(data.getDate()));

                boolean save = save(reservationSetting);
                if (!save){
                    throw  new DBException("[批量插入]新增插入失败");
                }
            }else {
                boolean update = this.lambdaUpdate().eq(ReservationSetting::getId, one.getId())
                        .set(ReservationSetting::getNumber, data.getNumber()).update();
                if (!update){
                    throw new DBException("[批量插入]失败了");
                }
            }
        }

    }

    /**
     * 用户查询可预约时间
     * @param month
     * @return
     */
    @Override
    public ReservationDateResDTO getReservationDateByMonth(String month) {
        List<String> reservationByMonth = reservationSettingMapper.getReservationDateByMonth(month);

        return new ReservationDateResDTO(reservationByMonth);
    }
}

